package com.zx.map.dao.impl;

import com.zx.map.dao.BaseAcctInfoDao;
import com.zx.map.dao.mapper.BaseAcctInfoMapper;
import com.zx.map.dao.model.BaseAcctInfo;
import com.zx.map.dao.model.BaseAcctInfoExample;
import com.zx.platform.dao.mapper.SequenceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 14-3-1
 * Time: 上午10:54
 * To change this template use File | Settings | File Templates.
 */
@Component("BaseAcctInfoDao")
public class BaseAcctInfoImpl implements BaseAcctInfoDao {

    @Autowired
    private BaseAcctInfoMapper mapper;

    @Autowired
    private SequenceMapper sequencemapper;

    /**
     * 新增
     */
    public String add(BaseAcctInfo baseAcctInfo) throws DataAccessException {
        baseAcctInfo.setCreateDate(new Date());
        int column = mapper.insert(baseAcctInfo);
        if (column != 1) {
            throw new RuntimeException("");
        }
        return baseAcctInfo.getCustId();
    }

    /**
     * 修改
     *
     * @param baseAcctInfo
     * @return
     * @throws DataAccessException
     */
    public String update(BaseAcctInfo baseAcctInfo) throws DataAccessException {
        int column = mapper.updateByPrimaryKeySelective(baseAcctInfo);
        if (column != 1) {
            throw new RuntimeException("");
        }
        return baseAcctInfo.getCustId();
    }

    /**
     * 删除
     */
    public void delete(BaseAcctInfo baseAcctInfo) throws DataAccessException {
        //PkA+sequence

        int column = mapper.deleteByPrimaryKey(baseAcctInfo.getCustId());
        if (column != 1) {
            throw new RuntimeException("");
        }
    }

    /**
     * 分页查找
     */
    public List<BaseAcctInfo> getPageByExample(int pageSize, int pageBegin,
                                               BaseAcctInfoExample example) throws DataAccessException {
        return mapper.getPageByModel(pageSize, pageBegin, example);
    }

    /**
     * 查找条数
     *
     * @param example
     * @return
     * @throws DataAccessException
     */
    public int countByExample(BaseAcctInfoExample example) throws DataAccessException {
        return mapper.countByExample(example);
    }

    /**
     *
     */
    public List<BaseAcctInfo> getListByExample(BaseAcctInfoExample example)
            throws DataAccessException {
        return mapper.selectByExample(example);
    }

    public BaseAcctInfo selectByPrimaryKey(String key) throws DataAccessException {
        return mapper.selectByPrimaryKey(key);
    }

    /**
     * 获取账务数据 并同时增加行锁
     */
    public BaseAcctInfo selectByPrimaryKeyAndLineLock(String key) throws DataAccessException {
        return mapper.selectByPrimaryKeyAndLineLock(key);
    }

    @Override
    public double countAvlbal(BaseAcctInfoExample example) {
        return mapper.countAvlbal(example);
    }

    @Override
    public double countFrzbal(BaseAcctInfoExample example) {
        return mapper.countFrzbal(example);
    }
}
